package com.lawfinal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lawfinal.entity.Lawcase;
import com.lawfinal.entity.Lawyerapply;
import com.lawfinal.entity.Writ;
import com.lawfinal.service.WritService;
import com.lawfinal.utils.QueryPageParam;
import com.lawfinal.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 仲裁 前端控制器
 * </p>
 *
 * @author KBr
 * @since 2024-05-09
 */
@RestController
@CrossOrigin("http://localhost:5173/")
@RequestMapping("/writ")
public class WritController {

      @Autowired
      private WritService writService;

      //      法律援助申请  增
      @RequestMapping("/apply")
      public Result apply(@RequestBody Writ writ){
            return writService.save(writ)?Result.sucess():Result.fail();
      }

      //     查
      @RequestMapping("/list")
      public List<Writ> list(){
            return writService.list();
      }

      @RequestMapping("/modifyState")
      public Result modifyState(@RequestBody Writ writ){
            return writService.updateById(writ)?Result.sucess():Result.fail();
      }

      //      分页基础数据
      @RequestMapping( "/listPageResult")
      public Result listPageResult(@RequestBody QueryPageParam queryPageParam){
            System.out.println("--------------------------");

            HashMap param=queryPageParam.getParam();
            System.out.println(param);


            Page<Writ> page = new Page();
            page.setCurrent(queryPageParam.getPageNum());
            page.setSize(queryPageParam.getPageSize());

            LambdaQueryWrapper<Writ> lambdaQueryWrapper= new LambdaQueryWrapper();
            IPage result=writService.page(page,lambdaQueryWrapper);

            System.out.println(result.getTotal());

            return Result.sucess(result.getRecords(),result.getTotal());
      }

      @RequestMapping("/update")
      public Result update(@RequestBody Writ writ){
            return  writService.updateById(writ)?Result.sucess():Result.fail();
      }

      @RequestMapping("findByUser")
      public Result findByUser( String user){
            List list=writService.lambdaQuery().eq(Writ::getPersona,user).list();
            return list.size()>0?Result.sucess(list):Result.fail();
      }
}
